<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/22
 * Time: 17:15
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Yuyue extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $yy_username = trim(input('yy_username'), ' ');
            $yy_phone = trim(input('yy_phone'), ' ');
            $yy_status = input('yy_status');
            $where = [];
            if(isset($yy_username) && $yy_username != ''){
                $where['yy_username'] = ['like', '%' . $yy_username . '%'];
            }
            if(isset($yy_phone) && $yy_phone != ''){
                $where['yy_phone'] = ['like', '%' . $yy_phone . '%'];
            }
            if(isset($yy_status) && $yy_status != ''){
                $where['yy_status'] = $yy_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('yuyue')->where($where)->count();// 获取总条数
            $count1 = Db::name('yuyue')->where($where)->column('yy_id');// 获取总条数
            $lists = Db::name('yuyue')
                ->where($where)
                ->field('yy_id, yy_username, yy_phone, yy_status, yy_do_time, yy_time')
                ->page($Nowpage,$limits)
                ->order('yy_time desc')
                ->select();
            $str = '';
            if($count1){
                $str = implode($count1, ',');
            }

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count1' => $str, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 标记已处理
     * @return \think\response\Json
     */
    public function yy_done(){
        $yy_id = input('id');
        $res = Db::table('up_yuyue')
            ->where('yy_id', $yy_id)
            ->update(['yy_status' => 2, 'yy_do_time' => date('Y-m-d H:i:s')]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        $ids = input('ids');
        if(empty($ids)) return json(['code' => 100, 'msg' => '请选择需要导出的数据']);

        $yy = Db::table('up_yuyue')
            ->where('yy_id', 'IN', $ids)
            ->field('yy_username, yy_phone, yy_status, yy_do_time, yy_time')
            ->select();
        foreach($yy as $k => $v){
            if($v['yy_status'] == 1){
                $yy[$k]['yy_status'] = '未处理';
            }else{
                $yy[$k]['yy_status'] = '已处理';
            }
            $yy[$k]['yy_phone'] = "\t".$v['yy_phone']."\t";
        }
        $cellname = [
            ['yy_username','姓名',20,'LEFT'],
            ['yy_phone','手机号',20,'LEFT'],
            ['yy_status','处理状态',20,'LEFT'],
            ['yy_time','提交时间',20,'LEFT'],
            ['yy_do_time','处理时间',20,'LEFT']
        ];
        $res = exportExcel('预约看房列表', 'Looks', $cellname, $yy);
        return json($res);
    }
}